package com.tutu.thymeleaf.controller;

import com.tutu.thymeleaf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

/**
 * @Program: SpringBoot
 * @Description: 循环
 * @Author: JK.TuTu
 * @ @Version: 1.0
 * @Create: 2022-11-30 13:50
 **/

@Controller
@RequestMapping("loop")
public class TestController03 {

    /**
     * 循环List
     * @param model
     * @return
     */
    @GetMapping("list")
    public String test1(Model model) {
        List<User> list = new ArrayList<>();
        list.add(new User("张小虎",18,new Date()));
        list.add(new User("陈小花",19,new Date()));
        list.add(new User("朱小妹",20,new Date()));
        list.add(new User("李小明",17,new Date()));
        list.add(new User("陈翠兰",21,new Date()));
        model.addAttribute("list",list);
        return "demo05";
    }


    /**
     * 循环数组
     * @param model
     * @return
     */
    @GetMapping("array")
    public String test2(Model model) {
        User[] arrays = new User[3];
        arrays[0] = new User("张小虎", 18, new Date());
        arrays[1] = new User("陈小花", 19, new Date());
        arrays[2] = new User("李小明", 20, new Date());
        model.addAttribute("arrays", arrays);
        return "demo06";
    }



    /**
     * 循环Map
     * @param model
     * @return
     */
    @GetMapping("map")
    public String test3(Model model) {
        Map<String,User> map = new HashMap<>();
        map.put("user1",new User("张小虎", 18, new Date()));
        map.put("user2",new User("陈小花", 19, new Date()));
        map.put("user3",new User("李小明", 20, new Date()));
        model.addAttribute("map", map);
        return "demo07";
    }



}
